package com.lmpop.trade.tb_goodstock;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.util.RandomID;
import com.lmpop.trade.tb_ordergoods.Tb_ordergoodsInfo;
import com.lmpop.trade.ti_gift.Ti_giftInfo;
import com.lmpop.trade.ti_goods.Ti_goodsInfo;
import com.lmpop.trade.ti_orderinfo.Ti_orderinfoInfo;
import com.lmpop.trade.ti_voucher.Ti_voucherInfo;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Tb_goodstockInfo
  implements TradeInterf
{
  QueryList queryList;

  public Tb_goodstockInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Tb_goodstock tb_goodstock = new Tb_goodstock();
    String trade_id = new RandomID().GenTradeId();
    tb_goodstock.setTrade_id(trade_id);
    tb_goodstock.setGoods_id(map.getString("goods_id"));
    tb_goodstock.setCust_id(map.getString("cust_id"));
    tb_goodstock.setOne_price(map.getString("one_price"));

    String one_price = map.getString("one_price");
    int now_num = map.getInt("now_num");
    float total_price = 0.0F;
    if (!one_price.equals(""))
    {
      total_price = Float.parseFloat(one_price) * now_num;
    }

    tb_goodstock.setTotal_price("" + total_price);
    tb_goodstock.setBefore_num(map.getInt("before_num"));
    tb_goodstock.setVary_num(map.getInt("vary_num"));
    tb_goodstock.setNow_num(map.getInt("now_num"));
    tb_goodstock.setVary_reason(map.getString("vary_reason"));

    tb_goodstock.setPublish_user_id(map.getString("publish_user_id"));
    String stock_method = map.getString("stock_method");
    String result_code = "0";
    try {
      if (!stock_method.equals("1"))
      {
        insert(dbmMgr, tb_goodstock);
      }
    }
    catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Tb_goodstock tb_goodstock)
    throws ApplicationException
  {
    dbmMgr.update("insertTb_goodstock", tb_goodstock);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
    dbmMgr.update("deleteTb_goodstock", pkid);
  }

  public SysMap deleteByGoodsId(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; deleteByGoodsId(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void deleteByGoodsId(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from tb_goodstock where goods_id=#value#
    dbmMgr.update("deleteTb_goodstockByGoodsId", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Tb_goodstock tb_goodstock = new Tb_goodstock();
    tb_goodstock.setTrade_id(map.getString("trade_id"));
    tb_goodstock.setGoods_id(map.getString("goods_id"));
    tb_goodstock.setCust_id(map.getString("cust_id"));
    tb_goodstock.setOne_price(map.getString("one_price"));
    tb_goodstock.setTotal_price(map.getString("total_price"));
    tb_goodstock.setBefore_num(map.getInt("before_num"));
    tb_goodstock.setVary_num(map.getInt("vary_num"));
    tb_goodstock.setNow_num(map.getInt("now_num"));
    tb_goodstock.setVary_reason(map.getString("vary_reason"));
    tb_goodstock.setRsrv_str1(map.getString("rsrv_str1"));
    tb_goodstock.setRsrv_str2(map.getString("rsrv_str2"));
    tb_goodstock.setRsrv_str3(map.getString("rsrv_str3"));
    tb_goodstock.setRsrv_str4(map.getString("rsrv_str4"));
    tb_goodstock.setRsrv_str5(map.getString("rsrv_str5"));
    tb_goodstock.setRsrv_str6(map.getString("rsrv_str6"));
    tb_goodstock.setPublish_date(map.getString("publish_date"));
    tb_goodstock.setPublish_user_id(map.getString("publish_user_id"));

    String result_code = "0";
    try {
      update(dbmMgr, tb_goodstock);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Tb_goodstock tb_goodstock)
    throws ApplicationException
  {
    dbmMgr.update("updateTb_goodstock", tb_goodstock);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Hashtable params = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    params.put("start", Integer.valueOf(start));
    params.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getListByTb_goodstockPage", params);
  }

  public int getCountByObj(Object obj)
  {
    Hashtable params = (Hashtable)obj;
    List list = this.queryList.getList("getCountByObjTb_goodstock", params);
    int count = 0;
    if (list != null)
    {
      count = list.size();
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTb_goodstockByPk", pk);
  }

  public Hashtable getGoodStockByGoodsId(String goods_id)
  {
    List sList = this.queryList.getList("getTb_goodstockByGoods", goods_id);
    Hashtable sMap = new Hashtable();
    if ((null != sList) && (sList.size() > 0))
    {
      sMap = (Hashtable)sList.get(0);
    }
    return sMap;
  }

  public List getAllGoodStockByGoodsId(String goods_id)
  {
    return this.queryList.getList("getTb_goodstockByGoods", goods_id);
  }

  public List getListByGoodsId(Object obj, int start, int limit)
  {
    Hashtable params = (Hashtable)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }

    params.put("start", Integer.valueOf(start));
    params.put("limit", Integer.valueOf(limit));
    return this.queryList.getList("getListByTb_goodstockGoodsId", params);
  }

  public int getCountByGoodsId(Object obj)
  {
    Hashtable params = (Hashtable)obj;
    List list = this.queryList.getList("getCountByObjTb_goodstockGoodsId", params);
    int count = 0;
    if ((list != null) && (list.size() > 0))
    {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public SysMap updateGoodsStock(DbmInterf dbmMgr, SysMap map)
  {
    if ((map.getString("order_state").equals("2")) || (map.getString("order_state").equals("5"))) {
      Ti_goodsInfo goodsInfo = new Ti_goodsInfo();
      String vary_reason = "";
      String order_no = map.getString("order_no");
      Ti_orderinfoInfo orderinfo = new Ti_orderinfoInfo();
      Ti_giftInfo ti_giftInfo = new Ti_giftInfo();
      Ti_voucherInfo ti_voucherInfo = new Ti_voucherInfo();
      Tb_ordergoodsInfo order_goods = new Tb_ordergoodsInfo();
      List order_list = order_goods.getOrderInfoByOP(order_no);
      if (map.getString("order_state").equals("2")) vary_reason = "��������Ʒ";
      if (map.getString("order_state").equals("5")) vary_reason = "�˻�����Ʒ";
      int orderSize = order_list.size();
      int vary_num = 0;
      String goods_id = ""; String publish_user_id = ""; String cust_id = "";
      String result_code = "0";
      if ((order_list != null) && (orderSize > 0)) {
        for (int i = 0; i < orderSize; i++) {
          Hashtable newsMap = (Hashtable)order_list.get(i);
          if ((newsMap.get("goods_id") != null) && (!newsMap.get("goods_id").toString().equals(""))) {
            goods_id = newsMap.get("goods_id").toString();
          }
          if ((newsMap.get("goods_num") != null) && (!newsMap.get("goods_num").toString().equals(""))) {
            vary_num = Integer.parseInt(newsMap.get("goods_num").toString());
          }
          if ((newsMap.get("user_id") != null) && (!newsMap.get("user_id").toString().equals(""))) {
            publish_user_id = newsMap.get("user_id").toString();
          }
          if ((newsMap.get("cust_id") != null) && (!newsMap.get("cust_id").toString().equals(""))) {
            cust_id = newsMap.get("cust_id").toString();
          }

          if ((newsMap.get("gift_id") != null) && (!"".equals(newsMap.get("gift_id").toString()))) {
            ti_giftInfo.updateGiftsStockNum(newsMap);
          }

          if ((newsMap.get("voucherinfos") != null) && (!"".equals(newsMap.get("voucherinfos")))) {
            String[] vouchers = null;
            String[] voucher_info = null;
            vouchers = newsMap.get("voucherinfos").toString().split("[|]");
            for (int j = 0; j < vouchers.length; j++) {
              voucher_info = vouchers[j].split("[:]");
              ti_voucherInfo.setVoucherStateByType(voucher_info[0], Integer.parseInt(voucher_info[1]));
            }
          }
          if (!goods_id.equals("")) {
            Map tb_goodstock = new Hashtable();
            String trade_id = new RandomID().GenTradeId();
            tb_goodstock.put("trade_id", trade_id);
            tb_goodstock.put("goods_id", goods_id);

            List old_stock_list = this.queryList.getList("getListGoodstockGoodsId", tb_goodstock);

            float one_price = 0.0F;
            int before_num = 0;
            int now_num = 0;
            if ((old_stock_list != null) && (old_stock_list.size() > 0))
            {
              Hashtable old_stockMap = (Hashtable)old_stock_list.get(0);
              if ((old_stockMap.get("one_price") != null) && (!old_stockMap.get("one_price").toString().equals(""))) {
                one_price = Float.parseFloat(old_stockMap.get("one_price").toString());
              }

              if ((old_stockMap.get("before_num") != null) && (!old_stockMap.get("before_num").toString().equals(""))) {
                before_num = Integer.parseInt(old_stockMap.get("before_num").toString());
              }

              if ((old_stockMap.get("now_num") != null) && (!old_stockMap.get("now_num").toString().equals(""))) {
                now_num = Integer.parseInt(old_stockMap.get("now_num").toString());
              }

            }

            tb_goodstock.put("cust_id", cust_id);
            tb_goodstock.put("one_price", Float.valueOf(one_price));

            tb_goodstock.put("before_num", Integer.valueOf(now_num));
            if (map.getString("order_state").equals("2")) vary_num = 0 - vary_num;
            now_num += vary_num;
            float total_price = now_num * one_price;
            tb_goodstock.put("total_price", Float.valueOf(total_price));
            tb_goodstock.put("vary_num", Integer.valueOf(vary_num));
            tb_goodstock.put("now_num", Integer.valueOf(now_num));
            tb_goodstock.put("vary_reason", vary_reason);
            tb_goodstock.put("publish_user_id", publish_user_id);

            Map ti_goods = new Hashtable();
            ti_goods.put("goods_id", goods_id);
            ti_goods.put("stock_out", Integer.valueOf(vary_num));
            try
            {
              updateGoodsStock(dbmMgr, tb_goodstock);
              goodsInfo.updateStockNumByOP(dbmMgr, ti_goods);
            }
            catch (ApplicationException e) {
              e.printStackTrace();
              result_code = "1";
            }
          }

        }

      }

      map.setString("result_code", result_code);
    }
    return map;
  }

  public void updateGoodsStock(DbmInterf dbmMgr, Map tb_goodstock)
    throws ApplicationException
  {
    dbmMgr.update("insertTb_goodstockByOP", tb_goodstock);
  }
}